//
//  NSString+Draw.m
//  Test
//
//  Created by xueshan1 on 2018/12/17.
//  Copyright © 2018 xueshan1. All rights reserved.
//

#import "NSString+Draw.h"

@implementation NSString (Draw)

- (CGSize)xs_sizeWithConstrainedSize:(CGSize)constrainedSize fontSize:(CGFloat)fontSize lineBreadMode:(NSLineBreakMode)lineBreadMode{
    
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineBreakMode = lineBreadMode;
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],
                          NSParagraphStyleAttributeName:paragraphStyle
                          };
    
    return [self boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
}

- (void)xs_drawWithContext:(CGContextRef)context rect:(CGRect)rect fontSize:(CGFloat)fontSize lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment color:(UIColor *)color{
    if (!color) {
        color = [UIColor blackColor];
    }
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = lineBreakMode;
    paragraphStyle.alignment = alignment;
    NSDictionary *attr = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],
                           NSForegroundColorAttributeName:color,
//                           NSBackgroundColorAttributeName:[UIColor blueColor],
                           NSParagraphStyleAttributeName:paragraphStyle
                           };
    [self drawInRect:rect withAttributes:attr];
    
}

@end
